package com.shkmall.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shkmall.common.pojo.PageResult;
import com.shkmall.goods.dao.BrandMapper;
import com.shkmall.goods.pojo.Brand;
import com.shkmall.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;
    /*查所有*/
    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectAll();

        return brands;
    }
/*添加*/
    @Override
    @Transactional
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }
/*修改*/
    @Override
    @Transactional
    public void update(Brand brand) {
       brandMapper.updateByPrimaryKeySelective(brand);
    }
    /*删除*/
    @Override
    @Transactional
    public void delete(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }
    /*条件查询*/
    @Override
    public List<Brand> searchCondition(Map searchMap) {


        if(!searchMap.isEmpty()){
            Example example = new Example(Brand.class);
            Example.Criteria criteria = example.createCriteria();

        /*条件封装*/
        /*进行数据判断*/
        if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
           criteria.andLike("name","%"+searchMap.get("name")+"%");
        }
        if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))) {
            criteria.andEqualTo("letter",searchMap.get("letter"));
            }
            return brandMapper.selectByExample(example);
    }else {
            return null;
        }
    }
    /*分页查询*/
    @Override
    public PageResult<Brand> findPage(Integer page, Integer size) {

        PageHelper.startPage(page,size);
        Page<Brand> data = (Page)brandMapper.selectAll();
        return new PageResult(data.getTotal(),data.getResult());
    }

    /*分页+条件查询*/
    @Override
    public PageResult<Brand> search(Map searchMap, Integer page, Integer size) {
        if(searchMap.isEmpty()){
            return null;
        }else {
            Example example = new Example(Brand.class);
            Example.Criteria criteria = example.createCriteria();
            if(searchMap.get("name")!=null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            if(searchMap.get("letter")!=null && !"".equals(searchMap.get("letter"))) {
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
            PageHelper.startPage(page,size);
            Page<Brand> data = (Page)brandMapper.selectByExample(example);
            return new PageResult<>(data.getTotal(),data.getResult());
        }


    }
    /*根据分类名称查询品牌列表*/
    @Override
    public List<Brand> findBrandByCategoryName(String categoryName) {
        return brandMapper.findBrandByCategoryName(categoryName);
    }


}
